package com.orange.util;

import java.util.Iterator;
import java.util.List;

import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.orange.lswidget.R;
import com.orange.lswidget.widgets.event.dao.UnreadSMSDao;
import com.orange.lswidget.widgets.event.manager.EventManager;
import com.orange.notificationsummary.utils.Constants;

public class LSEventUtils {
	private static final String	TAG				= "LSEventUtils";

	private static final String	COMMA			= ", ";
	private static final String	SPACE			= " ";

	private static String		sClassName		= "com.orange.lswidget.widgets.event.EventLSWidgetProvider";
	public static String		sPackageName	= "com.orange.lswidget";
	public static ComponentName	sComponentName	= new ComponentName(
														sPackageName,
														sClassName);

	public static String getNameString(Context context, List<String> names) {
		String ret = null;
		if (null != names && !names.isEmpty()) {
			StringBuffer retBuffer = new StringBuffer(context.getResources()
					.getString(R.string.from));
			retBuffer.append(SPACE);
			for (int count = 0; count < names.size(); count++) {
				LogHelper.i(TAG, "getNameString. names: " + names.get(count));

				retBuffer.append(names.get(count));
				if (count != names.size() - 1) {
					retBuffer.append(COMMA);
				}
			}
			ret = retBuffer.toString();
			LogHelper.d(TAG, "getNameString. ret: " + ret);
		}
		return ret;
	}

	public static String getNameString(Context context, List<String> names,
			String numberLabel) {
		String ret = getNameString(context, names);
		if (!TextUtils.isEmpty(ret) && !TextUtils.isEmpty(numberLabel)) {
			ret = ret + " (" + numberLabel + ")";
		}
		return ret;
	}

	public static void startNotificationSummary(Context context) {
		Intent intent = new Intent();
		intent.setAction(Constants.ACTION_NOTIFICATION);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		LSServiceUtils.startActivity(context, intent);
	}

	public static void sortEventInfo(EventManager[] eventInfos) {
		EventManager eventInfoTemp;
		int length = eventInfos.length;
		for (int i = 1; i < length; i++) {
			for (int j = length - 1; j >= i; j--) {
				long time = eventInfos[j - 1].getLastEventTime();
				long nextTime = eventInfos[j].getLastEventTime();
				if (time < nextTime) {
					eventInfoTemp = eventInfos[j - 1];
					eventInfos[j - 1] = eventInfos[j];
					eventInfos[j] = eventInfoTemp;
				}
			}
		}
	}

	public static void updateNamesByNumbers(Context context,
			List<String> fromNumbers, List<String> toNames) {
		if (null != fromNumbers && !fromNumbers.isEmpty()) {
			toNames.clear();

			Iterator<String> iterator = fromNumbers.iterator();
			UnreadSMSDao mMsgDao = new UnreadSMSDao(context);
			while (iterator.hasNext()) {
				String number = iterator.next();

				String tempName;
				if (number.equalsIgnoreCase("blocked")
						|| number.equalsIgnoreCase("unknown")) {
					tempName = context.getString(R.string.from_unknown);
				} else {
					tempName = mMsgDao.getDisplayNameByNumber(number);
				}

				if (!toNames.contains(tempName)) {
					toNames.add(tempName);
				}
			}
		}
	}

	// add by gaoge 2011-08-19 for fix bug 0013239
	public static int updateNotificationCount(Context context, int columnValue,
			int unreadmms_cnt, int miscall_cnt, int voicemail_cnt) {
		ContentResolver resolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		int ret = 0;

		values.put(EventManager.COLUMN_NOTIFICATION_COUNT, columnValue);

		values.put(EventManager.COLUMN_UNREADMMS_COUNT, unreadmms_cnt);
		values.put(EventManager.COLUMN_MISSCALL_COUNT, miscall_cnt);
		values.put(EventManager.COLUMN_VOICEMAI_COUNT, voicemail_cnt);

		String where = EventManager._ID + " = 1";
		ret = resolver.update(EventManager.CONTENT_URI, values, where, null);

		LogHelper.v(TAG, "updateNotificationCount." + " ,columnValue: "
				+ columnValue + " ,ret: " + ret);
		return ret;
	}

	public static int updateNotificationCount(Context context,
			String columnName, int columnValue) {
		ContentResolver resolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		int ret = 0;

		values.put(columnName, columnValue);
		String where = EventManager._ID + " = 1";
		ret = resolver.update(EventManager.CONTENT_URI, values, where, null);

		LogHelper.v(TAG, "updateNotificationCount. columnName: " + columnName
				+ " ,columnValue: " + columnValue + " ,ret: " + ret);
		return ret;
	}
}
